package com.hamster.tools.i18nManger.mediator
{
	import com.hamster.tools.i18nManger.commands.ConfigurationLoadCommand;
	import com.hamster.tools.i18nManger.commands.ConfigurationSaveCommand;
	import com.hamster.tools.i18nManger.commands.SaveNewItemsCommand;
	import com.hamster.tools.i18nManger.commands.ScanTargetFolderCommand;
	import com.hamster.tools.i18nManger.commands.TranslateAccessTokenCommand;
	import com.hamster.tools.i18nManger.commands.TranslateCommand;
	import com.hamster.tools.i18nManger.events.AppEvent;
	import com.hamster.tools.i18nManger.model.ConfigurationVOProxy;
	import com.hamster.tools.i18nManger.model.PropertyFileVOProxy;
	import com.hamster.tools.i18nManger.model.PropertyItemVOProxy;
	import com.hamster.tools.i18nManger.model.TranslateConfVOProxy;
	import com.hamster.tools.i18nManger.vo.ConfigurationVO;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class AppMediator extends Mediator
	{
		public static const NAME:String = "AppMediator";
		
		public function AppMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			facade.registerCommand(AppEvent.SCAN_TARGET_FOLDER_REQUEST, ScanTargetFolderCommand);
			facade.registerCommand(AppEvent.SAVE_NEW_ITEMS_REQUEST, SaveNewItemsCommand);
			facade.registerCommand(AppEvent.CONFIGURATION_SAVE_REQUEST, ConfigurationSaveCommand);
			facade.registerCommand(AppEvent.CONFIGURATION_LOAD_REQUEST, ConfigurationLoadCommand);
			facade.registerCommand(AppEvent.TRANSLATE_ACCESS_TOKEN_REQUEST, TranslateAccessTokenCommand);
			facade.registerCommand(AppEvent.TRANSLATE_REQUEST, TranslateCommand);
			facade.registerProxy(new PropertyFileVOProxy());
			facade.registerProxy(new PropertyItemVOProxy());
			facade.registerProxy(new ConfigurationVOProxy());
			facade.registerProxy(new TranslateConfVOProxy());
			
			app.addEventListener(AppEvent.SELECT_TARGET_FOLDER, selectTargetFolderHandler);
			app.addEventListener(AppEvent.SAVE_ALL_CHANGES_CLICK, saveAllChangesClickHandler);
			facade.sendNotification(AppEvent.CONFIGURATION_LOAD_REQUEST);
			facade.sendNotification(AppEvent.TRANSLATE_ACCESS_TOKEN_REQUEST);
		}
		
		override public function onRemove():void
		{
			
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppEvent.SCAN_TARGET_FOLDER_RESPONSE,
				AppEvent.SAVE_NEW_ITEMS_RESPONSE,
				AppEvent.CONFIGURATION_LOAD_RESPONSE,
				AppEvent.CONFIGURATION_LOAD_FAULT,
				AppEvent.CONFIGURATION_SAVE_RESPONSE,
				AppEvent.TRANSLATE_ACCESS_TOKEN_RESPONSE,
				AppEvent.TRANSLATE_ACCESS_TOKEN_FAULT,
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var body:Object = notification.getBody();
			var name:String = notification.getName();
			switch (name) {
				case AppEvent.SCAN_TARGET_FOLDER_RESPONSE:
					handleScanTargetFolderResponseHandler(body);
					break;
				case AppEvent.SAVE_NEW_ITEMS_RESPONSE:
					handleSaveNewItemsResponseHandler(body);
					break;
				case AppEvent.CONFIGURATION_LOAD_RESPONSE:
					handleConfigurationLoadResponseHandler(body);
					break;
				case AppEvent.CONFIGURATION_LOAD_FAULT:
					handleConfigurationLoadFaultHandler(body);
					break;
				case AppEvent.CONFIGURATION_SAVE_RESPONSE:
					handleConfigurationSaveResponseHandler(body);
					break;
				case AppEvent.TRANSLATE_ACCESS_TOKEN_RESPONSE:
					handleTranslateAccessTokenResponseHandler(body);
					break;
				case AppEvent.TRANSLATE_ACCESS_TOKEN_FAULT:
					handleTranslateAccessTokenFaultHandler(body);
					break;
			}
		}
		
		private function handleScanTargetFolderResponseHandler(data:Object):void
		{
			app.fileGroupVOList = data as ArrayCollection;
		}
		
		private function handleSaveNewItemsResponseHandler(data:Object):void
		{
			Alert.show("Successfully saved!");
		}
		
		private function handleConfigurationLoadResponseHandler(data:Object):void
		{
			app.statusText.text = "Configuration load success!";
		}
		
		private function handleConfigurationLoadFaultHandler(data:Object):void
		{
			app.statusText.text = "Configuration load failed!";
		}
		
		private function handleConfigurationSaveResponseHandler(data:Object):void
		{
			app.statusText.text = "Configuration save success!";
		}
		
		private function handleTranslateAccessTokenResponseHandler(data:Object):void
		{
			app.autoTranslateCheck.enabled = true;
		}
		
		private function handleTranslateAccessTokenFaultHandler(data:Object):void
		{
			app.autoTranslateCheck.selected = false;
			configurationVO.autoTranlate = "N";
		}
		
		private function selectTargetFolderHandler(event:AppEvent):void
		{
			configurationVO.rootFolder = event.data;
			var data:Object = {
				folder : event.data
			};
			
			this.sendNotification(AppEvent.SCAN_TARGET_FOLDER_REQUEST, data);
		}
		
		private function saveAllChangesClickHandler(event:AppEvent):void
		{
			this.sendNotification(AppEvent.SAVE_NEW_ITEMS_REQUEST);
			this.sendNotification(AppEvent.CONFIGURATION_SAVE_REQUEST);
		}
		
		public function get configurationVO():ConfigurationVO
		{
			return (facade.retrieveProxy(ConfigurationVOProxy.NAME) as ConfigurationVOProxy).configurationVO;
		}
		
		public function get app():HsI18NManager
		{
			return viewComponent as HsI18NManager;
		}
	}
}